import * as THREE from 'three'
import Camera from './Camera'
import Renderer from './Renderer'
import Sizes from '../Utils/Sizes'
import Time from '../Utils/Time'
import World from '../World/World'
import assets from '../Utils/Assets'
import Resource from '../Utils/Resource'
import Preloader from './Preloader'
export default class Experience {
  static instance: Experience
  canvas!: HTMLCanvasElement
  wrapper!: HTMLElement
  sizes!: Sizes
  time!: Time
  camera!: Camera
  renderer!: Renderer
  scene!: THREE.Scene
  resource!: Resource
  world!: World
  preloader!: Preloader
  constructor(canvas?: HTMLCanvasElement, wrapper?: HTMLElement) {
    if (Experience.instance || !canvas || !wrapper) {
      return Experience.instance
    }
    Experience.instance = this
    this.canvas = canvas
    this.wrapper = wrapper
    this.sizes = new Sizes(this.canvas, this.wrapper)
    this.time = new Time()
    this.scene = new THREE.Scene()
    this.camera = new Camera()
    this.renderer = new Renderer()
    this.resource = new Resource(assets)
    this.world = new World()
    this.preloader = new Preloader()
    this.sizes.on('resize', () => {
      this.resize()
    })
    this.time.on('update', () => {
      this.update()
    })
  }
  resize() {
    this.camera.resize()
    this.renderer.resize()
  }
  update() {
    this.camera.update()
    this.renderer.update()
  }
}
